/**
 * Copyright 2011 Honeywell Federal Manufacturing & Technologies, LLC. This material was produced under U.S. Government
 * contract DE-ACO4-01AL66850, Honeywell Federal Manufacturing & Technologies, LLC, (FM&T) with the U. S. Department of
 * Energy for the operation of the Kansas City Plant . The U.S. Government has rights to use, reproduce, and distribute
 * this software.  NEITHER THE GOVERNMENT NOR Honeywell Federal Manufacturing & Technologies, LLC, MAKES ANY WARRANTY,
 * EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE.  If software is modified to produce
 * derivative works, such modified software should be clearly marked, so as not to confuse it with the version available
 * from FM&T.
 *
 * Additionally, this program is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License Version 3 published by the Free Software Foundation. Accordingly, this program is distributed in
 * the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this library.
 * If not, see http://www.gnu.org/licenses/.
 */
package org.braintrain.services;

import com.kcp.ko.fmtnmlib.tools.defensive.Defensive;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.braintrain.domain.DeltaResource;
import org.braintrain.domain.ResourceFacade;
import org.springframework.beans.factory.annotation.Required;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @author John Ericksen
 */
public class DeltaResourceManager {

    private static final Logger LOG = Logger.getLogger(DeltaResourceManager.class);

    private ResourceManager resourceManager;

    /**
     * Builds and returns the resource value as a String.
     *
     * Delta = false returns current published contents
     * Delta = true returns latest changes.
     *
     * @param resource
     * @param delta
     * @return resource value
     */
    public String getResourceValue(DeltaResource resource, boolean delta) {
        if(resource == null){
            return null;
        }

        return getResourceValue(resource.getValue(delta));
    }

    /**
     * Builds and returns the given resource value as a String
     *
     * @param resource
     * @return resource value
     */
    public String getResourceValue(ResourceFacade resource) {
        if(resourceManager.isEmpty(resource)){
            return null;
        }

        String resourceValue;

        try {
            InputStream resourceIS = resourceManager.getStream(resource);
            resourceValue = IOUtils.toString(resourceIS);
        } catch (Exception e) {
            LOG.error(e);
            resourceValue = null;
        }

        return resourceValue;
    }

    /**
     * Builds and pipes the resource value to an OutputStream.
     *
     * Delta = false returns current published contents
     * Delta = true returns latest changes.
     *
     * @param outputStream
     * @param resource
     * @param delta
     */
    public void getResourceValue(OutputStream outputStream, DeltaResource resource, boolean delta) {
        if(resource != null){
            getResourceValue(outputStream, resource.getValue(delta));
        }
    }

    /**
     * Builds and pipes the resource value to an OutputStream.
     *
     * @param outputStream
     * @param resource
     */
    public void getResourceValue(OutputStream outputStream, ResourceFacade resource) {
        if(!resourceManager.isEmpty(resource)){
            try {
                InputStream resourceIS = resourceManager.getStream(resource);
                IOUtils.copy(resourceIS, outputStream);
            } catch (Exception e) {
                LOG.error(e);
            }

        }
    }

    /**
     * Saves the provided input value to the given resource taking into account
     * defined visioning rules.
     *
     * @param input
     * @param resource
     */
    public void saveResourceValue(String input, DeltaResource resource) {
        Defensive.notNull(resource, "Delta Resource");

        //generate next delta
        ResourceFacade delta = resource.nextDeltaValue();

        saveResourceValue(input, delta);
    }

    /**
     * Saves the provided input value to the given resource.
     *
     * @param input
     * @param resourceFacade
     */
    public void saveResourceValue(String input, ResourceFacade resourceFacade) {
        Defensive.notNull(resourceFacade, "Resource Facade");

        saveResourceValue(new ByteArrayInputStream(input.getBytes()), resourceFacade);
    }

    /**
     * Saves the provided input stream value tot he given resource.
     *
     * @param input
     * @param resourceFacade
     */
    public void saveResourceValue(InputStream input, ResourceFacade resourceFacade) {
        Defensive.notNull(resourceFacade, "Resource Facade");

        if (input != null)
        {
            resourceManager.saveResourceValue(input, resourceFacade);
            

        }
    }

    @Required
    public void setResourceManager(ResourceManager resourceManager) {
        this.resourceManager = resourceManager;
    }
}
